import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    token: "",
    userInfo: JSON.parse(sessionStorage.getItem("userInfo")),
    studentInfo: JSON.parse(sessionStorage.getItem("studentInfo")),
    query: ""
  },
  mutations: {
    // set
    SET_TOKEN: (state, token) => {
      state.token = token;
      localStorage.setItem("token", token);
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo;
      sessionStorage.setItem("userInfo", JSON.stringify(userInfo));
    },
    REMOVE_INFO: state => {
      state.token = "";
      state.userInfo = {};
      localStorage.setItem("token", "");
      sessionStorage.setItem("userInfo", JSON.stringify(""));
    },
    SET_STUDENTINFO: (state, studentInfo) => {
      state.studentInfo = studentInfo;
      sessionStorage.setItem("studentInfo", JSON.stringify(studentInfo));
    },
    SET_QUERY: state => {
      state.query = true;
      sessionStorage.setItem("query", JSON.stringify(true));
    }
  },
  getters: {
    // get
    getUser: state => {
      return state.userInfo;
    },
    getStudent: state => {
      return state.studentInfo;
    },
    getQuery: state => {
      return state.query;
    }
  },
  actions: {},
  modules: {}
});
